<template>
  <div class="app-container">
    <!-- 查询和其他操作 -->
    <div class="filter-container">
      <el-input v-model="listQuery.keyWord" clearable class="filter-item" style="width: 200px; margin-right: 10px" placeholder="请输入查询词" @keyup.enter.native="handleFilter"/>
      <el-button class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">查找</el-button>
      <router-link :to="{path:'/administration/adClass/edit'}" style="margin-left: 10px">
        <el-button class="filter-item" type="primary" icon="el-icon-edit">添加</el-button>
      </router-link>
      <el-button class="filter-item" type="primary" icon="el-icon-edit" @click="handleBatchCreate" style="margin-left: 10px">批量添加</el-button>
    </div>

    <!-- 查询结果 -->
    <el-table v-loading="listLoading" :data="list" element-loading-text="正在查询中。。。" row-key="id"  border fit highlight-current-row style="width: 100%">
      <el-table-column label="行政班名" prop="name"/>
      <el-table-column label="所属院系" prop="department"/>
      <el-table-column label="专业" prop="major"/>
      <el-table-column align="center" label="操作" width="200">
        <template slot-scope="{row}">
          <router-link :to="{path:'/administration/adClass/edit', query:{id: row.id}}" class="link-left">
            <el-button size="mini">编辑</el-button>
          </router-link>
          <el-button type="danger" class="link-left" size="mini" @click="handleDelete(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.pageIndex" :limit.sync="listQuery.pageSize" @pagination="getList" />
  </div>
</template>

<script>
import administrationApi from '@/api/administration'
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination

export default {
  name: 'Student',
  components: { Pagination },
  data () {
    return {
      list: [],
      total: 0,
      listQuery: {
        pageIndex: 1,
        pageSize: 20,
        keyWord: undefined
      },
      listLoading: true,
      uploadUrl: '/api/admin/student/upload'
    }
  },
  created () {
    this.getList()
  },
  methods: {
    getList () {
      this.listLoading = true
      administrationApi.listClass(this.listQuery).then(response => {
        this.list = response.response.list
        this.total = response.response.total
        this.listLoading = false
      }).catch(() => {
        this.list = []
        this.total = 0
        this.listLoading = false
      })
    },
    handleFilter () {
      this.listQuery.pageIndex = 1
      this.getList()
    },
    handleCreate () {
      this.listQuery.pageIndex = 1
      this.getList()
    },
    handleBatchCreate () {
      administrationApi.batchCreateClass().then(response => {
        this.$message.success('添加成功')
        this.getList()
      }).catch(() => {
        this.$message.error('添加失败')
      })
    },
    handleDelete (row) {
      let _this = this
      administrationApi.deleteClass(row.id).then(re => {
        if (re.code === 1) {
          _this.getList()
          _this.$message.success(re.message)
        } else {
          _this.$message.error(re.message)
        }
      })
    }
  }
}
</script>
